desc="""lay out the six buttons into two rows of three columns each -- to do that, you should
use a wxGridSizer.
In the following example we use two nested sizers, the main one with vertical layout
and the embedded one with horizontal layout:"""
#Editor.py
import wx
import os
import Sapi5
tts = pi5.Create( {"name":"Mary"})
purge = tts._purge
async = tts._async
MN=0
ID=1
HK=2
KD=3
MF=4
MF2=5
class MainWindow(wx.Frame):
    def __init__(self, parent, id, title):
        self.dirname=os.getcwd()  #SEARCH FROM PRESENT DIRECTORY!
        self.filename=""
        self.items4menu = {"File": [
            {MN:"Open", ID:102, HK:"&Open", KD:" Open a file to edit", MF:self.OnOpen},
            {MN:"Save", ID:103, HK:"&Save", KD:" save file to disk", MF:self.OnSave},
            {MN:"Edit", ID:104, HK:"&Edit", KD:" Do editing", MF:self.OnEdit},
            {MN:"About", ID:101, HK:"&About", KD:" Information about this program", MF:self.OnAbout},
            {MN:"Exit", ID:109, HK:"E&xit", KD:" Terminate the program", MF:self.OnExit}
        ],  #END OF FILE MENU!
        "Voice": [
            {MN:"Read", ID:202, HK:"&Read", KD:" Open a file to read", MF:self.OnWav2Read},
            {MN:"Save", ID:203, HK:"&Save", KD:" save text to audio file", MF:self.OnSave2Wav},
            {MN:"Text", ID:204, HK:"Te&xt", KD:" read text field", MF:self.OnRead},
            {MN:"Quit", ID:205, HK:"&Quit", KD:" Stop Reading", MF:self.OnQuitRead},
        ]  #END OF VOICE MENU!
        }  #END OF ITEMS FOR MENU!
        self.buttons4voice = [
            {MN:"Voice", ID:111, HK:"&Voice", KD:" Voice Change", MF:self.OnEnter, MF2:self.OnClick},
            {MN:"Rate", ID:112, HK:"&Rate", KD:" Rate For Voice Adjust", MF:self.OnEnter, MF2:self.OnClick},
            {MN:"Pitch", ID:113, HK:"&Pitch", KD:" Pitch Of Voice Adjust", MF:self.OnEnter, MF2:self.OnClick},
            {MN:"Volume", ID:114, HK:"&Loudness", KD:" Volume Loudness Adjust", MF:self.OnEnter, MF2:self.OnClick}
        ]  #END OF ITEM FOR BUTTONS!
        wx.Frame.__init__(self, parent, wx.ID_ANY, title)
        self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
        self.CreateStatusBar()  #A Statusbar in the bottom of the window
        #Setting up the menu.
        filemenu = wx.Menu()
        for o in self.items4menu["File"]:
            filemenu.Append( o[ID], o[HK], o[KD])
            filemenu.AppendSeparator()
        voicemenu = wx.Menu()
        for o in self.items4menu["Voice"]:
            voicemenu.Append( o[ID], o[HK], o[KD])
            filemenu.AppendSeparator()
        # Creating the menubar.
        menuBar = wx.MenuBar()
        menuBar.Append( filemenu,"&File") # Adding the "filemenu" to the MenuBar
        menuBar.Append( voicemenu,"&Voice") # Adding the "voicemenu" to the MenuBar
        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
        self.data4menu = {}
        for o in self.items4menu["File"]:
            wx.EVT_MENU(self, o[ID], o[MF])
            self.data4menu[ o[ID]] = o
        for o in self.items4menu["Voice"]:
            wx.EVT_MENU(self, o[ID], o[MF])
            self.data4menu[ o[ID]] = o
        self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        self.buttons=[]
        self.data4buttons={}
        i=0
        for o in self.buttons4voice:
            self.buttons.append( wx.Button(self, o[ID], o[HK]+o[KD]))
            self.sizer2.Add( self.buttons[i], 1, wx.EXPAND)
            self.data4buttons[ self.buttons[i].GetId()] = o
            self.buttons[i].Bind(wx.EVT_ENTER_WINDOW, o[MF]) #, id=self.buttons[i].GetId())
            self.buttons[i].Bind( wx.EVT_RIGHT_DOWN, o[MF2]) #, id=self.buttons[i].GetId())
            self.buttons[i].Bind( wx.EVT_BUTTON, o[MF2]) #, id=self.buttons[i].GetId())
            i+=1
        # Use some sizers to see layout options
        self.sizer=wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.sizer2,0,wx.EXPAND)
        self.sizer.Add(self.control,1,wx.EXPAND)
        #Layout sizers
        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)
        self.Show(1)
    def OnEnter(self, event):
        "WHEN ENTERING SAY LABEL OF BUTTON!"
#        self.button2bind = self.Bind(wx.EVT_BUTTON, self.OnClick, id=event.GetId())
        eventType = event.GetEventType()
#        tts.Speak( event.GetPosition())
        label4btn = self.data4buttons[ event.GetId()][0]
#        tts.Speak( label4btn)
        if label4btn == "Voice":
            set_value = tts.getVoiceName()
        elif label4btn == "Rate":
            set_value = str( tts.getRate())
        elif label4btn == "Pitch":
            set_value = str( tts.getPitch())
        elif label4btn == "Volume":
            set_value = str( tts.getVolume())
        text = label4btn +" Button " +set_value
        tts.Speak( text, async, purge)
    def OnClick(self, event):
        "BUTTON CLICKED ON AND IN FUTURE GET BUTTON POS!"
#comment        tts.Speak( 'Event Name: %s Time Stamp: %s ' % (event.GetClassName(), event.GetTimestamp()))
#comment        print 'Event Name: %s Time Stamp: %s ' % (event.GetClassName(), event.GetTimestamp())
#comment IF KEY EVENT:
#comment        key = event.GetKeyCode()
#comment        pos = event.GetPositionTuple()
        eventType = event.GetEventType()
#comment        if eventType == wx.EVT_BUTTON: print "Mouse! "
        eventName = event.GetClassName()
        dir = 1
        if eventName == "wxMouseEvent": dir = -1
        label4btn = self.data4buttons[ event.GetId()][0]
        set_value = "Error! Wrong Button!"
        if label4btn == "Voice":
            value = tts.getVoiceNum() + dir
            if value < 1:               value += 0
            if value >= tts.getVoiceCount():
                value = tts.getVoiceCount()-1
            tts.setVoice( value)
            set_value = tts.getVoiceName()
        elif label4btn == "Rate":
            value = tts.getRate()
            value += dir
            if value > 10: value = 10
            if value < -10: value = -10
            tts.setRate( value)
            set_value = str( value)
        elif label4btn == "Pitch":
            value = tts.getPitch()
            value += dir
            if value > 10: value = 10
            if value < -10: value = -10
            tts.setPitch( value)
            set_value = str( value)
        elif label4btn == "Volume":
            value = tts.getVolume()
            value += 10*dir
            if value > 100: value = 100
            if value < 0:
                tts.setVolume( 50)
                tts.Speak( "Volume Minimum!")
                value = 0
            tts.setVolume( value)
            set_value = str( value)
        tts.Speak( set_value, async)
    def OnAbout(self,e):
        "A dialog box saying what the editor is about!"
        text = " A sample editor with voice \n in wxPython."
        tts.Speak( "About A Sample Editor!"+text)
        d= wx.MessageDialog( self, text, "About Sample Editor", wx.OK)
                            # Create a message dialog box
        d.ShowModal() # Shows it
        d.Destroy() # finally destroy it when finished.
    def OnExit(self,e):
        self.Close(True)  # Close the frame.
    def OnOpen(self,e):
        """ Open a file"""
        self.setFilePath( "o")
        f=open(os.path.join(self.dirname, self.filename),'r')
        self.control.SetValue(f.read())
        f.close()
    def OnSave(self,e):
        """ Save a file"""
        self.setFilePath( "s")
        f=open(os.path.join(self.dirname, self.filename),'w')
#       self.control.SetValue(f.read())
        f.write( self.control.GetValue())
        f.close()
    def setFilePath(self, type4file=""):
        " Search directory for path and file name!"
        fn=self.filename
        t4f = wx.OPEN
        if type4file[0] in "sS":
            t4f = wx.SAVE|wx.FD_OVERWRITE_PROMPT
#            fn = self.filename
        dlg = wx.FileDialog(self, self.filename +" or Choose a file", self.dirname, fn, "*.*", t4f)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
        dlg.Destroy()
    def OnWav2Read(self,e):
        """ Open a file to read"""
        self.setFilePath( "o")
        tts.SpeakFromWav( os.path.join(self.dirname, self.filename), async, purge)
    def OnSave2Wav(self,e):
        """ Save text to a wav file"""
        self.setFilePath( "s")
        tts.SpeakToWav( os.path.join(self.dirname, self.filename), self.control.GetValue())
    def OnRead(self,e):
        """ Read the text"""
        tts.Speak( self.control.GetValue(), async, purge)
    def OnQuitRead(self,e):
        """ Quit the reading of the text"""
        tts.Speak( " Talking Stopped!", purge)
    def OnEdit(self,e):
        """ Edit the file"""
        self.control.SetFocus()
app = wx.PySimpleApp()
frame = MainWindow(None, -1, "Sample editor")
app.MainLoop()
desc2="""The sizer.Add
 method has three arguments. The first one specifies the control to include in the
sizer. The second one is a weight factor which means that this control will be sized
in proportion to other ones. For example, if you had three edit controls and you
wanted them to have the proportions 3:2:1 then you would specify these factors as
arguments when adding the controls. 0 means that this control or sizer will not grow.
The third argument is normally
wxGROW (same as wxEXPAND
) which means the control will be resized when necessary. If you use
wxSHAPED instead, the controls aspect ratio will remain the same.
If the second parameter is 0, i.e. the control will not be resized, the third parameter
may indicate if the control should be centered horizontally and/or vertically by
using wxALIGN_CENTER_HORIZONTAL, wxALIGN_CENTER_VERTICAL, or wxALIGN_CENTER
 (for both) instead of
wxGROW or wxSHAPED as that third parameter.
You can alternatively specify combinations of wxALIGN_LEFT, wxALIGN_TOP, wxALIGN_RIGHT,
and wxALIGN_BOTTOM. The default behavior is equivalent to
wxALIGN_LEFT | wxALIGN_TOP."""
